package com.ktjiaoyu.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjiaoyu.crm.config.Constants;
import com.ktjiaoyu.crm.pojo.Role;
import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.service.IRoleService;
import com.ktjiaoyu.crm.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private IRoleService roleService;


    @PostMapping("/login")
    public String doLogin(Model model, HttpSession session, String usrName, String usrPassword){
        System.out.println("用户名："+usrName+"密码："+usrPassword);
        User user=userService.login(usrName,usrPassword);
        if(user==null){
            model.addAttribute("msg","用户名或密码错误");
            System.out.println("用户名或密码错误");
            return "login";
        }
        session.setAttribute(Constants.USER_SESSION,user);
        return "redirect:/main";
    }


    @PostMapping("/del/{id}")
    @ResponseBody
    public String deleteUser(@PathVariable Integer id) {
        if (id == null || id == 0) {
            return "false";
        }
        int count = userService.deleteUser(id);
        if (count == 0) {
            return "false";
        } else {
            return "true";
        }
    }




    @GetMapping("/getUser")
    public User getUser(Integer usrId) {
        User user = userService.getUserById(usrId);
        return user;
    }


    @GetMapping("/Users")
    public List<User> getUsers() {
        return userService.findAllUsers();
    }


    @GetMapping("user/{id}")
    public User getUserById(@PathVariable Integer id) {
        return userService.getUserById(id);
    }


    @GetMapping("/add")
    public String addUser(Model model) {
        // 获取所有角色列表
        List<Role> roles = roleService.list();
        // 将角色列表添加到模型中，以便在页面中展示
        model.addAttribute("roles", roles);
        return "/user/add";
    }

    @PostMapping("/save")
    public String saveUser(User user) {
        int i = userService.saveUser(user);
        if (i > 0) {
            return "redirect:/user/list";
        }
        return "/user/add";
    }


    @GetMapping("/check")
    @ResponseBody
    public String checkUser(String usrName) {
        if (usrName!= null &&!"".equals(usrName)) {
            return "false";
        }
        List<User> list = userService.list(new QueryWrapper<User>().eq("usr_name", usrName));
        if (list.size() > 0) {
            return "false";
        } else {
            return "true";
        }
    }


    @GetMapping("/edit")
    public String updateUser(Model model, Long usrId) {
        // 获取所有角色列表
        List<Role> roles = roleService.list();
        // 根据用户 ID 获取用户信息
        User user = userService.getUserById(Math.toIntExact(usrId));
        // 将角色列表和用户信息添加到模型中，以便在页面中展示
        model.addAttribute("roles", roles);
        model.addAttribute("user", user);
        return "/user/edit";
    }


    @PostMapping("/edit")
    public String editUser(User user) {
        int i = userService.saveUser(user);
        if (i > 0) {
            return "redirect:/user/list";
        }
        return "/user/edit";
    }


    @RequestMapping("/list")
    public String list(Model model, String usrName, @RequestParam(required = false, defaultValue = "0") Integer roleId,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        Sort sort = Sort.by(Sort.Direction.ASC, "usrId");
        Pageable pageable= PageRequest.of(pageIndex - 1, 5, sort);
        return "user/list";
    }
}
